package com.shallowUniverse.process.mapper.provider;

import com.shallowUniverse.process.context.sql.SqlSentenceUtil;
import com.shallowUniverse.process.entity.ProcessNodeDefinition;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Map;

/**
 * @author gs
 * @date create in 2021/1/17 14:28
 */
public class ProcessNodeDefinitionProvider {

    public String selectById(String id) {
        return SqlSentenceUtil.generateSentence("selectProcessNodeById");
    }

    public String selectFirtstNode(Long processId) {
        return SqlSentenceUtil.generateSentence("selectFirstProcessNodeByProcess");
    }

    public String selectNextNode(ProcessNodeDefinition processNodeDefinition) {
        return SqlSentenceUtil.generateSentence("selectNextProcessNode");
    }

    public String selectFieldValue(Map<String, String> parmas) {
        return SqlSentenceUtil.generateSentence("selectFieldValue");
    }

    public String insert(ProcessNodeDefinition processNodeDefinition) {
        return SqlSentenceUtil.generateSentence("insertProcessNode");
    }

    public String selectList(ProcessNodeDefinition processNodeDefinition) {
        //查询字段和条件字符
        String[] entityFields = {"name", "startDate", "endDate", "roleId", "deptId", "processId", "seq"};
        String[] sqlFields = {"ppn.name", "ppn.create_date", "ppn.create_date", "ppn.role_id", "ppn.dept_id", "ppn.process_id", "seq"};
        String[] signs = {"like", ">=", "<=", "=", "=", "=", "="};
        return SqlSentenceUtil.generateSentenceWithFieldConditions("selectProcessNodes", processNodeDefinition, entityFields, sqlFields, signs);
    }

    public String selectMaxNodeByRole(Map<String, String> params) {
        return SqlSentenceUtil.generateSentence("selectProcessNodeByProcessRole");
    }

    public String insertBatch(List<ProcessNodeDefinition> processNodeDefinitions) {
        return new SQL() {
            {
                INSERT_INTO("ps_process_node");
                processNodeDefinitions.iterator().forEachRemaining(processNodeDefinition -> {
                    String sign = processNodeDefinition.getSign() == null ? "null" : "'" + processNodeDefinition.getSign() + "'";
                    String field = processNodeDefinition.getField() == null ? "null" : "'" + processNodeDefinition.getField() + "'";
                    INTO_VALUES("null", "'" + processNodeDefinition.getName() + "'", processNodeDefinition.getRoleId() + "",
                            processNodeDefinition.getDeptId() + "", processNodeDefinition.getSeq() + "", "'" + processNodeDefinition.getRemark() + "'",
                            processNodeDefinition.getProcessId() + "", sign, field,
                            processNodeDefinition.getCount() + "", "'" + processNodeDefinition.getCreateDate() + "'", "1");
                    ADD_ROW();
                });
            }
        }.toString();
    }

    public String update(ProcessNodeDefinition processNodeDefinition) {
        return SqlSentenceUtil.generateSentence("updateProcessNodeById");
    }

    public String deleteLogic(String id) {
        return SqlSentenceUtil.generateSentence("deleteProcessNodeLogic");
    }

    public String delete(String id) {
        return SqlSentenceUtil.generateSentence("deleteProcessNode");
    }

    public String deleteByProcessLogic(String processId) {
        return SqlSentenceUtil.generateSentence("deleteProcessNodeByProcessLogic");
    }

    public String deleteByProcess(String processId) {
        return SqlSentenceUtil.generateSentence("deleteProcessNodeByProcess");
    }
}
